package com.project.logistics.controller;

import com.project.logistics.bean.Repairs;
import com.project.logistics.bean.User;
import com.project.logistics.service.ApplicantService;
import com.project.logistics.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.Collection;
import java.util.List;

/**
 * Created by94988 on 2021/3/16 16:45
 */
@Controller
public class UserController {

    @Autowired
    UserService userService;



//    @RequestMapping("/app/main")
//    public String mainPage(){
//        return "/app/main";
//    }

//    @PostMapping ("/user/login")
    @RequestMapping("/user")
    public String userLogin(){
        return "/signin";
    }

    @RequestMapping("/user/login")
    public String userLogin(User user,HttpSession session, Model model){
//        User user = new User(userId,password,1);
//        if(user!=null){
//            User user1 = userService.findUser(user.getUserId());
//            System.out.println(user1.toString());
//            if (user1 != null) {
//                session.setAttribute("user", user1);
//                return "redirect:/app/main";
//            }
//        }
        User user1 = userService.findUser(user.getUserId());
        if (user1 != null && user1.getPassword().equals(user.getPassword())){
            System.out.println(user1.toString());
            session.setAttribute("user", user1);
            int type = user1.getUserType();
            switch (type){
                case 1 :
                    return "redirect:/app/main";
                case 2:
                    return "redirect:/repman/main";
                default:
                    return "redirect:/adm/main";
            }

        }
        model.addAttribute("msg","账号为空");
        return "signin";
    }

    @GetMapping(value = {"/"})
    public String login(HttpSession session){
        return "index";
    }

}
